﻿using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;

namespace PlutoStudio.Logging
{
    internal class CastleLoggerProvider : ILoggerProvider
    {
        private readonly Castle.Core.Logging.ILoggerFactory _castleLoggerFactory;

        private readonly ConcurrentDictionary<string, CastleLoggerAdapter> _loggers;
        
        public CastleLoggerProvider(Castle.Core.Logging.ILoggerFactory castleLoggerFactory)
        {
            _castleLoggerFactory = castleLoggerFactory;
            _loggers = new ConcurrentDictionary<string, CastleLoggerAdapter>();
        }
        
        public ILogger CreateLogger(string categoryName)
        {
            return _loggers.GetOrAdd(
                categoryName,
                name => new CastleLoggerAdapter(_castleLoggerFactory.Create(name))
            );
        }
        
        public void Dispose()
        {
        }
    }
}